package com.chenailin.www.daoimpl;

import com.chenailin.www.dao.StudentDAO;
import com.chenailin.www.pojo.Student;
import com.chenailin.www.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class StudentDAOImpl implements StudentDAO {

    // 修改学生
    @Override
    public boolean modifyStudent(Student student) throws SQLException {
        String sql = "UPDATE students SET name = ?, tel = ?, age = ? WHERE id = ?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setString(1, student.getStuName());
            ps.setString(2, student.getStuTel());
            ps.setInt(3, student.getStuAge());
            ps.setInt(4,student.getStuId());
            //受影响的行数
            int affectedRows = ps.executeUpdate();
            return affectedRows > 0;
        } catch (SQLException e) {
            System.err.println("数据库操作失败: " + e.getMessage());
            return false;
        }
    }

    // 修改密码
    @Override
    public boolean modifyPassword(Student student) throws SQLException {
        String sql = "UPDATE students SET password = ? WHERE id = ?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setString(1, student.getPassword());
            ps.setInt(2,student.getStuId());
            //受影响的行数
            int affectedRows = ps.executeUpdate();
            return affectedRows > 0;
        } catch (SQLException e) {
            System.err.println("数据库操作失败: " + e.getMessage());
            return false;
        }
    }

    // 根据编号查找学生
    @Override
    public Student findById(int id) {
        String sql = "SELECT * FROM students WHERE id = ?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setInt(1, id);
            try (ResultSet rs = ps.executeQuery()){
                if(rs.next()) {
                    return new Student(rs.getInt("age"),
                             rs.getInt("id"),
                             rs.getString("name"),
                             rs.getString("tel"),
                             rs.getBoolean("verified"));
                }
                return null;
            }
        } catch (SQLException e) {
            System.err.println("数据库操作失败: " + e.getMessage());
            return null;
        }
    }

    // 身份验证
    @Override
    public boolean verifyStudent(int id) {
        String sql = "UPDATE students SET verified = ? WHERE id = ?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setBoolean(1,true);
            ps.setInt(2,id);
            //受影响的行数
            int affectedRows = ps.executeUpdate();
            return affectedRows > 0;
        } catch (SQLException e) {
            System.err.println("数据库操作失败: " + e.getMessage());
            return false;
        }
    }

}
